<?php

class Plugin_AccessCheck extends Zend_Controller_Plugin_Abstract{
	private $_acl = null;
	private $_auth = null;

	public function __construct(Zend_Acl $acl, Zend_Auth $auth){

		$this->_acl = $acl;
		$this->_auth = $auth;
	}

	public function preDispatch(Zend_Controller_Request_Abstract $request)
	{
		$module = ($request->getModuleName() <> "default")? $request->getModuleName()."/" : "default/" ;
		$resource = $request->getControllerName();
		$action = $request->getActionName();
		$s_Sass = new Zend_Session_Namespace('s_Sass');
		$b_Conf = new Zend_Session_Namespace('b_Config');

		if (strtoupper($resource)=="LOGIN" && strtoupper($action)=="LOGOUT"){

		}
		else{
			$identity = @$this->_auth->getStorage()->read();

			if(Zend_Auth::getInstance()->hasIdentity()){

				$Facade = new Default_Model_Facade_Userinfo();
				$To = new Default_Model_TO_Useraccess();
				$To->setUser_Id($identity->Id);
				$data=$Facade->getPersonalInfo($To->getAllVars());
				 
				foreach ($data as $dt){
					$s_Sass->User_Id 	=   $dt['User_Id'];
					$s_Sass->Username 	=	$dt['Username'];
					$s_Sass->Role 		=   $dt['Role'];
					$s_Sass->Email 		=	$dt['Email'];
					$s_Sass->Picture_Id =  	$dt['Picture_Id'];
				}
			}
			
			$b_Conf->Email_Site = 'oriotiori.tk@gmail.com';
			$b_Conf->Title_Site = 'rockalterno';

		}
		
		$toResource = new Default_Model_TO_Resource();
		$fModuleType = new Default_Model_Facade_ModuleResources();
		$StrResource = "";
		$StrResource =$module . $resource .  "/" . $action;

		$toResource->setDescription($StrResource);

		$data_ModuleType = $fModuleType->ReadModuleByResource($toResource->getAllVars());
		
		$moduleto= (preg_replace(array('/\//'), '', $module)=='admin') ? preg_replace(array('/\//'), '', $module) : 'default';

		$fUserAccess = new Default_Model_Facade_Userinfo();
		$To = new Default_Model_TO_Useraccess();

		/**		if(($resource == "Login"  && $action="index")
		 || ($resource == "index"  && $action="index")
		 || (strtoupper($resource)=="LOGIN" && strtoupper($action)=="LOGOUT")
		 || (strtoupper($resource)=="LOGIN" && strtoupper($action)=="SWITCH"))
		 {
		 echo "";
		 }
		 **/

		if($data_ModuleType[0]['Id']!=0)
		{
			if(count($data_ModuleType)>0){
				foreach($data_ModuleType as $dtmod)
				{
					$idModule = $dtmod['Id'];
					$To->setUser_Id($s_Sass->User_Id);
					$To->setModule_Id($idModule);
					
					if($fUserAccess->isAllow($To->getAllVars()))
					{
						
					}
					else{
						$s_Sass->redirectafterloginorregister=$toResource->getDescription();
						
						if(Zend_Auth::getInstance()->hasIdentity()){
							$request->setModuleName($moduleto)->setControllerName('login')->setActionName('noaccess');
						}
						else{						
							$request->setModuleName($moduleto)->setControllerName('login')->setActionName('index');
						}
					}
				}
			}
			else{
				$s_Sass->redirectafterloginorregister=$toResource->getDescription();
				$request->setModuleName($moduleto)->setControllerName('login')->setActionName('index');
			}
		}
	}
}

